package com.wsz.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsz.system.pojo.User;
import com.wsz.system.service.MenuService;
import com.wsz.system.service.UserService;
import com.wsz.system.mapper.UserMapper;
import com.wsz.vo.RouterVo;
import com.wsz.vo.SysUserQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wangs
* @description 针对表【sys_user(用户表)】的数据库操作Service实现
* @createDate 2022-11-08 20:58:27
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private MenuService menuService;

    @Override
    public Map<String, Object> getUserInfo(String username) {

        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.eq("username",username);
        User user = baseMapper.selectOne(queryWrapper);

        //查询用户菜单权限
        List<RouterVo> RouterVoList = menuService.getUserMenuList(user.getId());
        //查询用户按钮权限
        List<String> permsList = menuService.getUserButtonList(user.getId());

        Map<String,Object> map = new HashMap<>();

        map.put("name",username);
        map.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        map.put("roles","[\"admin\"]");
        map.put("routers",RouterVoList);
        map.put("buttons",permsList);
        return map;
    }

    @ApiOperation("用户条件分页查询")
    @Override
    public IPage<User> selectPage(Page<User> page1, SysUserQueryVo sysUserQueryVo) {
        String keyword = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(keyword)){
            queryWrapper.like("username",keyword).or()
                    .like("name",keyword).or()
                    .like("phone",keyword);
        }
        if(StringUtils.isNotBlank(createTimeBegin)){
            queryWrapper.ge("create_time",createTimeBegin);
        }
        if(StringUtils.isNotBlank(createTimeEnd)){
            queryWrapper.le("update_time",createTimeEnd);
        }

        Page<User> userPage = baseMapper.selectPage(page1, queryWrapper);
        return userPage;
    }

    //"通过id修改其状态"
    @Override
    public Boolean updateStatusById(String id, Integer status) {
        User user = baseMapper.selectById(id);
        user.setStatus(status);
        int i = baseMapper.updateById(user);
        if(i > 0) return true;
        else return false;
    }
}




